/*
import { defineStore } from 'pinia'
import { ref } from 'vue'

// 模拟API调用
const mockApi = {
  fetchAddresses: () => new Promise(resolve => {
    setTimeout(() => {
      resolve([
        {
          id: 1,
          name: '张三',
          phone: '13800138000',
          province: '北京市',
          city: '北京市',
          district: '海淀区',
          detail: '中关村南大街5号',
          isDefault: true
        },
        {
          id: 2,
          name: '李四',
          phone: '13900139000',
          province: '上海市',
          city: '上海市',
          district: '浦东新区',
          detail: '张江高科技园区',
          isDefault: false
        }
      ])
    }, 500)
  }),
  addAddress: (address) => new Promise(resolve => {
    setTimeout(() => {
      resolve({ ...address, id: Date.now() })
    }, 300)
  }),
  updateAddress: (address) => new Promise(resolve => {
    setTimeout(() => {
      resolve(address)
    }, 300)
  }),
  deleteAddress: (id) => new Promise(resolve => {
    setTimeout(() => {
      resolve(true)
    }, 300)
  }),
  setDefaultAddress: (id) => new Promise(resolve => {
    setTimeout(() => {
      resolve(true)
    }, 300)
  })
}

export const useAddressStore = defineStore('address', () => {
  const addresses = ref([])
  const loading = ref(false)
  const error = ref(null)

  const fetchAddresses = async () => {
    loading.value = true
    error.value = null
    try {
      const data = await mockApi.fetchAddresses()
      addresses.value = data
    } catch (err) {
      error.value = '获取地址列表失败'
      console.error(err)
    } finally {
      loading.value = false
    }
  }

  const addAddress = async (address) => {
    try {
      const newAddress = await mockApi.addAddress(address)
      addresses.value.push(newAddress)
      if (newAddress.isDefault) {
        setDefaultAddress(newAddress.id)
      }
    } catch (err) {
      error.value = '添加地址失败'
      console.error(err)
      throw err
    }
  }

  const updateAddress = async (address) => {
    try {
      const updatedAddress = await mockApi.updateAddress(address)
      const index = addresses.value.findIndex(a => a.id === updatedAddress.id)
      if (index !== -1) {
        addresses.value[index] = updatedAddress
        if (updatedAddress.isDefault) {
          setDefaultAddress(updatedAddress.id)
        }
      }
    } catch (err) {
      error.value = '更新地址失败'
      console.error(err)
      throw err
    }
  }

  const deleteAddress = async (id) => {
    try {
      await mockApi.deleteAddress(id)
      addresses.value = addresses.value.filter(a => a.id !== id)
    } catch (err) {
      error.value = '删除地址失败'
      console.error(err)
      throw err
    }
  }

  const setDefaultAddress = async (id) => {
    try {
      await mockApi.setDefaultAddress(id)
      addresses.value = addresses.value.map(a => ({
        ...a,
        isDefault: a.id === id
      }))
    } catch (err) {
      error.value = '设置默认地址失败'
      console.error(err)
      throw err
    }
  }

  return {
    addresses,
    loading,
    error,
    fetchAddresses,
    addAddress,
    updateAddress,
    deleteAddress,
    setDefaultAddress
  }
})
*/
